﻿using System;

namespace LearnFile4
{
    class Program
    {
        static void Main(string[] args)
        {
            // C# string.Format格式化日期
            DateTime dt = new DateTime(2017, 4, 1, 13, 16, 32, 108);
            Console.WriteLine(string.Format("{0:y yy yyy yyyy}", dt)); //17 17 2017 2017
            Console.WriteLine(string.Format("{0:M MM MMM MMMM}", dt)); //4  04 四月 四月
            Console.WriteLine(string.Format("{0:d dd ddd dddd}", dt)); //1  01 周六 星期六
            Console.WriteLine(string.Format("{0:t tt}", dt)); //下 下午
            Console.WriteLine(string.Format("{0:H HH}", dt)); //13 13
            Console.WriteLine(string.Format("{0:h hh}", dt)); //1  01
            Console.WriteLine(string.Format("{0:m mm}", dt)); //16 16
            Console.WriteLine(string.Format("{0:s ss}", dt)); //32 32
            Console
                .WriteLine(string
                    .Format("{0:F FF FFF FFFF FFFFF FFFFFF FFFFFFF}", dt)); //1 1  108 108  108   108    108
            Console
                .WriteLine(string
                    .Format("{0:f ff fff ffff fffff ffffff fffffff}", dt)); //1 10 108 1080 10800 108000 1080000
            Console.WriteLine(string.Format("{0:z zz zzz}", dt)); //+8 +08 +08:00

            Console.WriteLine(string.Format("{0:yyyy/MM/dd HH:mm:ss.fff}", dt)); //2017/04/01 13:16:32.108
            Console.WriteLine(string.Format("{0:yyyy/MM/dd dddd}", dt)); //2017/04/01 星期六
            Console
                .WriteLine(string.Format("{0:yyyy/MM/dd dddd tt hh:mm}", dt)); //2017/04/01 星期六 下午 01:16
            Console.WriteLine(string.Format("{0:yyyyMMdd}", dt)); //20170401
            Console.WriteLine(string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", dt)); //2017-04-01 13:16:32.108
            Console.ReadLine();

            // *.ToString()也可以实现相同的效果：
            DateTime dti = new DateTime(2017, 4, 1, 13, 16, 32, 108);
            Console.WriteLine(dti.ToString("y yy yyy yyyy")); //17 17 2017 2017
            Console.WriteLine(dti.ToString("M MM MMM MMMM")); //4  04 四月 四月
            Console.WriteLine(dti.ToString("d dd ddd dddd")); //1  01 周六 星期六
            Console.WriteLine(dti.ToString("t tt")); //下 下午
            Console.WriteLine(dti.ToString("H HH")); //13 13
            Console.WriteLine(dti.ToString("h hh")); //1  01
            Console.WriteLine(dti.ToString("m mm")); //16 16
            Console.WriteLine(dti.ToString("s ss")); //32 32
            Console.WriteLine(dti.ToString("F FF FFF FFFF FFFFF FFFFFF FFFFFFF")); //1 1  108 108  108   108    108
            Console.WriteLine(dti.ToString("f ff fff ffff fffff ffffff fffffff")); //1 10 108 1080 10800 108000 1080000
            Console.WriteLine(dti.ToString("z zz zzz")); //+8 +08 +08:00

            Console.WriteLine(dti.ToString("yyyy/MM/dd HH:mm:ss.fff")); //2017/04/01 13:16:32.108
            Console.WriteLine(dti.ToString("yyyy/MM/dd dddd")); //2017/04/01 星期六
            Console.WriteLine(dti.ToString("yyyy/MM/dd dddd tt hh:mm")); //2017/04/01 星期六 下午 01:16
            Console.WriteLine(dti.ToString("yyyyMMdd")); //20170401
            Console.WriteLine(dti.ToString("yyyy-MM-dd HH:mm:ss.fff")); //2017-04-01 13:16:32.108
        }
    }
}
